package com.ah.shanxi_demo.sync.client;

import com.ah.shanxi_demo.entity.SxRegister;
import com.ah.http.httpsclient.MyHttpsClient;
import com.ah.http.httpsclient.constants.HttpHeaderContentTypeEnum;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

import static com.ah.shanxi_demo.sync.convert.ConvertSyncParam.convert_sync_register;
import static com.ah.shanxi_demo.sync.convert.ConvertSyncRequest.convert_sync_request_header;
import static com.ah.shanxi_demo.sync.utils.syncResultConvertObject.sync_result_convert_centerKey;

/**
 * @author zt 2020/8/15
 */
public class SyncRegisterClient {
    private static final Logger log = LoggerFactory.getLogger(SyncRegisterClient.class);


    /**
     * 部级平台 注册接口 客户端
     * @param sxRegister
     * @return
     */
    public static String register_client(SxRegister sxRegister) {

        //部级平台进行注册
        String serviceAddress = sxRegister.getServiceAddress();
        Object responseObject = sdk_register(sxRegister, serviceAddress);

        //解析返回值节点唯一标识字段
        String centerKey = sync_result_convert_centerKey(responseObject);

        return centerKey;
    }

    /**
     * 注册接口_SDK
     *
     * @param param
     * @param ServiceAddress
     * @return
     */
    public static Object sdk_register(SxRegister param, String ServiceAddress) {

        //地址
        String url = ServiceAddress;

        //参数
        Map<String, Object> paramsMap = convert_sync_register(param);

        //请求头
        Map<String, String> headerMap = convert_sync_request_header(paramsMap);

        try {

            log.info("sync_register【url】：" + JSON.toJSONString(url));
            log.info("sync_register【paramsMap】：" + JSON.toJSONString(paramsMap));
            log.info("sync_register【headerMap】：" + JSON.toJSONString(headerMap));

            Object responseObject = MyHttpsClient.sendPostHttps(url, paramsMap, headerMap, HttpHeaderContentTypeEnum.JSON.getKey());

            log.info("sync_register【responseObject】：" + JSON.toJSONString(responseObject));
            return responseObject;
        } catch (Exception e) {
            log.error("sync_register【Exception】：" + JSON.toJSONString(e));
        }
        return null;
    }

}
